package com.miplus.ccrm.core.service.aiqm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.aiqm.*;
import com.miplus.ccrm.core.pojo.QmRules;
import com.miplus.ccrm.core.mapper.QmRulesMapper;
import com.miplus.ccrm.core.pojo.QmTemplate;
import com.miplus.ccrm.core.pojo.QmTemprulesLink;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【QM_RULES(质检规则表)】的数据库操作Service实现
* @createDate 2024-11-03 19:05:14
*/
@Service
public class QmRulesServiceImpl extends MPJBaseServiceImpl<QmRulesMapper, QmRules>
    implements QmRulesService{
    @Override
    public IPage<QmRulesResVo> qmRulesList(QmRulesReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                QmRulesResVo.class,
                new MPJLambdaWrapper<QmRules>()
                        .selectAs("fun_getnames('QM.DATA.ROLE',t.qmrole)",QmRulesResVo::getQmroledsc)
                        .selectAs("fun_getnames('QM.DATA.RANGE',t.qmrange)",QmRulesResVo::getQmrangedsc)
                        .eq(JPubUtils.IsNotEmpty(paras.getPmopt()),QmRules::getPmopt,paras.getPmopt())
                        .like(JPubUtils.IsNotEmpty(paras.getRname()),QmRules::getRname,paras.getRname())
                        .selectAll(QmRules.class)
                        .orderByAsc(QmRules::getRulesid)
        );
    }

    @Override
    public  CmdResVo qmRulesAdd(String $usrid,QmRulesAddVo paras)
    {
        QmRules def=new QmRules();
        BeanUtils.copyProperties(paras,def);
        def.setCrdt(new Date());
        def.setCrusr($usrid);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.insert(def);
        return new CmdResVo(ret,def.getRulesid().toString());
    }

    @Override
    public CmdResVo qmRulesEdit(String $usrid,QmRulesAddVo paras)
    {
        QmRules def=new QmRules();
        BeanUtils.copyProperties(paras,def);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.updateById(def);
        return new CmdResVo(ret,def.getRulesid().toString());
    }

    @Override
    public CmdResVo qmRulesDelete(Integer rulesid)
    {
        int ret=baseMapper.deleteById(rulesid);
        return new CmdResVo(ret,rulesid.toString());
    }

    @Override
    public List<LabelResVo> getRulesList(Integer tempid)
    {
        return selectJoinList(LabelResVo.class,
                new MPJLambdaWrapper<QmRules>()
                        .selectAs(QmRules::getRulesid,LabelResVo::getValue)
                        .selectAs(QmRules::getRname,LabelResVo::getLabel)
                        .leftJoin(QmTemprulesLink.class,"link",QmTemprulesLink::getRulesid,QmRules::getRulesid)
                        .eq(QmTemprulesLink::getTemplateid,tempid)
        );
    }

    @Override
    public List<QmRulesResVo> getRulesDetailList(Integer tempid)
    {
        return selectJoinList(QmRulesResVo.class,
                new MPJLambdaWrapper<QmRules>()
                        .leftJoin(QmTemprulesLink.class,"link",QmTemprulesLink::getRulesid,QmRules::getRulesid)
                        .eq(QmTemprulesLink::getTemplateid,"tempid")
                        .selectAll(QmRules.class)
            );
    }
}




